'use strict';

angular.module('app')
    .controller('updateController', ['$scope', '$rootScope', '$state', '$timeout',
        function($scope, $rootScope, $state, $timeout) {
            var title = "";
            $scope.imgData={};
            if($state.includes('**.label.update')){
                title="编辑标签";
                var labelNo = $state.params.labelNo;
                detail(labelNo);
                $scope.isDisabled = false;
            }else if($state.includes('**.label.create')){
                title="添加标签";
//                $scope.record = {'bannerType' :1};
                $scope.isDisabled = false;
                $scope.flagShowOrHide=1;//用来控制排序号和是否可用是否显示
            }
            $scope.title = $rootScope.title = title;
            $scope.loading = true;

            //初始化验证
            $scope.submit= function(){
                $scope.loading = true;

                if(!$('#labelType').val()){
                    $scope.alertModal({'content':'标签类型不能为空'});
                    return;
                }


                if(!$('#labelName').val()){
                    $scope.alertModal({'content':'标签名称不能为空'});
                    return;
                }

                saveData();
            };

            function saveData(){
                var m = $scope.record;
                console.log(m);
                if(m){
                    $scope.isDisabled = true;//提交disabled
                    $.ajax({
                        url : $scope.record.labelNo? $scope.app.project_name+'/label/update' : $scope.app.project_name+'/label/add',
                        data: m
                    }).then(callback);
                }
                function callback(result){
                    if(result.httpCode ==200){
                        $scope.alertModal({"content":"操作成功"});
                        $timeout(function(){
                            $state.go('main.communityConfig.label.list');
                        },2000);
                    }else{
                        $scope.alertModal({"content":"操作失败"});
                        $scope.isDisabled = false;
                    }
                    $scope.loading = false;
                }
            }
                $scope.labelType = [{
                    id: 1,
                    Name: '帖子'
                }, {
                    id: 2,
                    Name: '用户'
                }];
            // 初始化页面
            function detail(labelNo) {
                $scope.loading = true;
                $.ajax({
                    url : $scope.app.project_name + '/label/read/list',
                    data: {labelNo: labelNo}
                }).then(function(result) {
                    $scope.loading = false;
                    if (result.httpCode == 200) {
                        $scope.record = result.data.records[0];
                        $scope.imgData = [];
                        var srcArr = [];
                        srcArr.push($scope.record.imgUrl);
                        $scope.imgData.push({
                            src:srcArr
                        })
                        console.log($scope.imgData)
                    } else {
                        $scope.msg = result.msg;
                    }
                    $scope.$apply();
                });
            }

        }]);